package com.itheima.youdianda.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.youdianda.common.Result;
import com.itheima.youdianda.domain.SysMenu;
import com.itheima.youdianda.domain.SysRoleMenu;
import com.itheima.youdianda.dto.SysMenuDto;
import com.itheima.youdianda.service.SysMenuService;
import com.itheima.youdianda.service.SysRoleMenuService;
import com.itheima.youdianda.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-09-07-0:36
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @PostMapping("/update")
    //@Caching(
    //        evict = {@CacheEvict(cacheNames = "allMenus", keyGenerator = "commonKeyGenerator"), @CacheEvict(cacheNames = "menuInfo", key = "#sysMenu.id")}
    //)
    public Result update(@RequestBody SysMenu sysMenu){
        sysMenuService.updateById(sysMenu);
        return Result.ok("修改成功");
    }


    @GetMapping("/del")
    //@Caching(
    //        evict = {@CacheEvict(cacheNames = "allMenus", keyGenerator = "commonKeyGenerator"), @CacheEvict(cacheNames = "menuInfo", key = "#id")}
    //)
    public Result deleteMenu(@RequestParam String id){
        sysMenuService.removeById(id);
        LambdaQueryWrapper<SysRoleMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysRoleMenu::getMenuId, id);
        sysRoleMenuService.remove(queryWrapper);
        return Result.ok("删除成功");
    }

    @GetMapping("/allData")
    //@Cacheable(cacheNames = "allMenus", keyGenerator = "commonKeyGenerator")
    public Result getAllMenus(){
        List<SysMenuDto> list = sysMenuService.getAllMenus();
        return Result.ok(list);
    }

    @PostMapping("/add")
    //@CacheEvict(cacheNames = "allMenus", keyGenerator = "commonKeyGenerator")
    public Result add(@RequestBody SysMenu sysMenu, HttpSession session){
        String id = (String) session.getAttribute("userId");
        String[] roleIds = sysUserService.getById(id).getRoleId()
                .replace("[", "")
                .replace("]", "")
                .replace("\"", "")
                .split(",");
        sysMenuService.save(sysMenu);
        sysRoleMenuService.add(sysMenu.getId(), roleIds);
        return Result.ok("添加成功");
    }

    @GetMapping("/rowInfo")
    //@Cacheable(cacheNames = "menuInfo", key = "#id")
    public Result getMenuInfo(@RequestParam String id){
        SysMenu byId = sysMenuService.getById(id);
        return Result.ok(byId);
    }
}
